﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace Mota.XMLControls
{
    public class DBRadioGroup:RadioGroup
    {

        private bool autoBuild = true;

        public bool AutoBuild
        {
            get
            {
                return this.autoBuild;
            }
            set
            {
                this.autoBuild = value;
            }
        }
        #region Data Binding
        private CurrencyManager currencyManager = null;
        private String valueMember;
        private String displayMember;
        private object oDataSource;

        public object DataSource
        {
            get
            {
                return oDataSource;

            }
            set
            {
                if (value == null)
                {
                    currencyManager = null;
                    Properties.Items.Clear();
                }
                else
                {
                    if (!(value is IList || oDataSource is IListSource))
                        throw (new System.Exception("Invalid DataSource"));
                    else
                    {
                        if (value is IListSource)
                        {
                            IListSource myListSource = (IListSource)value;
                            //if collection of collections throw exception
                            if (myListSource.ContainsListCollection == true)
                                throw new Exception("Invalid DataSource");
                        }
                        oDataSource = value;
                        currencyManager = (CurrencyManager)this.BindingContext[value];
                        if (this.AutoBuild)
                            BuildRadioGroup();
                    }
                }
            }
        }

        public string ValueMember
        {
            get
            {
                return this.valueMember;
            }
            set
            {
                this.valueMember = value;
            }
        }

        public string DisplayMember
        {
            get
            {
                return this.displayMember;
            }
            set
            {
                this.displayMember = value;
            }
        }

        public object GetValue(int index)
        {
            IList innerList = this.currencyManager.List;
            if (innerList != null)
            {
                if ((this.ValueMember != "") && (index >= 0 && index < innerList.Count))
                {
                    PropertyDescriptor pdValueMember;
                    pdValueMember = this.currencyManager.GetItemProperties()[this.ValueMember];
                    return pdValueMember.GetValue(innerList[index]);
                }
            }
            return null;
        }

        public object GetDisplay(int index)
        {
            IList innerList = this.currencyManager.List;
            if (innerList != null)
            {
                if ((this.DisplayMember != "") && (index >= 0 && index < innerList.Count))
                {
                    PropertyDescriptor pdDisplayMember;
                    pdDisplayMember = this.currencyManager.GetItemProperties()[this.DisplayMember];
                    return pdDisplayMember.GetValue(innerList[index]);
                }
            }
            return null;
        }

        #endregion

        public void BuildRadioGroup()
        {
            RadioGroupItemCollection items = this.Properties.Items;
            items.Clear();
            if ((this.currencyManager != null) && (this.currencyManager.List != null))
            {
                IList innerList = this.currencyManager.List;

                this.Properties.BeginUpdate();
                for (int i = 0; i < this.currencyManager.Count; i++)
                {
                    this.currencyManager.Position = i;

                    PropertyDescriptor pdValue;
                    PropertyDescriptor pdDisplay;

                    pdValue = this.currencyManager.GetItemProperties()[this.ValueMember];
                    pdDisplay = this.currencyManager.GetItemProperties()[this.DisplayMember];

                    object currObject = innerList[this.currencyManager.Position];
                    object nodeValue = pdValue.GetValue(this.currencyManager.Current);
                    String displayValue = pdDisplay.GetValue(this.currencyManager.Current).ToString();
                    items.Add(new RadioGroupItem(nodeValue, displayValue));
                }
                this.Properties.EndUpdate();

                if (items.Count > 0)
                    //select first node
                    this.SelectedIndex = 0;
            }
        }
    }
}
